﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GraphNeural : MonoBehaviour
{
    public int layerIndex;
    public int neuralIndex;
    public NeuralNetwork nn;
    public bool isBias;
    private MeshRenderer mr;
    public float v;
    private void Start()
    {
        mr = GetComponent<MeshRenderer>();
        mr.material = new Material(Shader.Find("Legacy Shaders/Self-Illumin/Diffuse"));

        if (layerIndex > 0)
        {
            GraphNeuralNetwork gnn = transform.parent.GetComponent<GraphNeuralNetwork>();
            float zf = gnn.graphNeuralLayerList[layerIndex - 1].GraphNeuralList[0].neuralObj.transform.position.z;
            float zt = gnn.graphNeuralLayerList[layerIndex - 1].GraphNeuralList[gnn.graphNeuralLayerList[layerIndex - 1].GraphNeuralList.Count - 1].neuralObj.transform.position.z;
            int num = gnn.graphNeuralLayerList[layerIndex].GraphNeuralList.Count;
            float zm = (zf + zt) / 2;

            if (num % 2 == 1)
            {
                int mIndex = num / 2;
                float z = (neuralIndex - mIndex) * 5 + zm;
                transform.position = new Vector3(transform.position.x, transform.position.y, z);
            }
            else
            {
                int mIndex = num / 2;
                float z = (neuralIndex - mIndex + 0.5f) * 5 + zm;
                transform.position = new Vector3(transform.position.x, transform.position.y, z);
            }
        }
    }
    void LateUpdate()
    {
        if (nn == null) return;
        if (!isBias)
        {
            v = (float)(nn.neuralLayers[layerIndex].neurals[neuralIndex].value);
            if (v > 0)
            {
                mr.material.color = Color.Lerp(Color.black, Color.green, v);
            }
            else
            {
                mr.material.color = Color.Lerp(Color.black, Color.red, -v);
            }
        }
        else
        {
            mr.material.color = Color.green;
        }
    }
}
